import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class MethodChannelDemo extends StatefulWidget {
  @override
  _MethodChannelDemoState createState() {
    return _MethodChannelDemoState();
  }
}

class _MethodChannelDemoState extends State<MethodChannelDemo> {
  static const MethodChannel _methodChannel = MethodChannel(
    'samples.flutter.io/method_channel',
  );
  late var response;
  late var msg;

  @override
  void initState() {
    super.initState();
    _methodChannel.setMethodCallHandler((call) async{
      switch(call.method){
        case 'callFlutterMethodName':{
          setState(() {
            msg=call.arguments;
          });
        }
      }

    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(children: [
        ElevatedButton(onPressed: _callNative, child: Text("调用native的方法")),
        Text(response),
        Text('原生调用flutter传递的参数${msg}')
      ]),
    );
  }

  Future<void> _callNative() async {
     response=await _methodChannel.invokeListMethod("callNativeMethodName","参数信息");
  }
}
